<?php
/**
 * @copyright 
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */
 
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Device as DeviceModel;
use app\admin\validate\DeviceValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Device extends BaseController

{
	/**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new DeviceModel();
		$this->uid = get_login_admin('id');
    }
    /**
    * 数据列表
    */
    public function datalist()
    {
        if (request()->isAjax()) {
			$param = get_params();
			$where = [];
			$where[] = ["delete_time","=",0];
            if (!empty($param['device_name'])) {
                $where[] = ['device_name','like',"%{$param['device_name']}%"];
            }
            if (!empty($param['imei'])) {
                $where[] = ['imei','=',$param['imei']];
            }
            if (!empty($param['msgid'])) {
                $where[] = ['msgid','=',$param['msgid']];
            }
            if (!empty($param['status'])) {
                $where[] = ['status','=',$param['status']];
            }
            if (!empty($param['user_one'])) {
                $one_ids = Db::name('user_one')
                    ->where([['user_name','like',"%{$param['user_one']}%"],['delete_time','=',0]])
                    ->column('id');
                $where[] = ['user_one','in',$one_ids];
            }
            if (!empty($param['user_two'])) {
                $two_ids = Db::name('user_two')
                    ->where([['user_name','like',"%{$param['user_two']}%"],['delete_time','=',0]])
                    ->column('id');
                $user_one_ids = Db::name('user_one')
                    ->where([['pid','in',$two_ids],['delete_time','=',0]])
                    ->column('id');
                $where[] = ['user_one','in',$user_one_ids];
            }
            if (!empty($param['user_three'])) {
                $three_ids = Db::name('user_three')
                    ->where([['user_name','like',"%{$param['user_three']}%"],['delete_time','=',0]])
                    ->column('id');
                $where[] = ['user_three','in',$three_ids];
            }
            $list = $this->model->getDeviceList($where,$param);
            return table_assign(0, '', $list);
        }
        else{
            return view();
        }
    }

    /**
    * 添加
    */
    public function add()
    {
        if (request()->isAjax()) {		
			$param = get_params();	
			
            // 检验完整性
            try {
                validate(DeviceValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
			
            $this->model->addDevice($param);
        }else{
			return view();
		}
    }
	

    /**
    * 编辑
    */
    public function edit()
    {
		$param = get_params();
        if (request()->isAjax()) {

            // 检验完整性
            try {
                validate(DeviceValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }
			
            $this->model->editDevice($param);
        }else{
			$id = isset($param['id']) ? $param['id'] : 0;
			$detail = $this->model->getDeviceById($id);
			if (!empty($detail)) {
                $userOneList = Db::name('user_one')->where(['status'=>1,'delete_time'=>0])->order('id desc')->select();
                $userThreeList = Db::name('user_three')->where(['status'=>1,'delete_time'=>0])->order('id desc')->select();
                $cateList = Db::name('cate')->where(['delete_time'=>0])->order('sort desc')->select();
                View::assign('cateList',$cateList);
                View::assign('userThreeList',$userThreeList);
                View::assign('userOneList',$userOneList);
				View::assign('detail', $detail);
				return view();
			}
			else{
				throw new \think\exception\HttpException(404, '找不到页面');
			}			
		}
    }


    /**
    * 查看信息
    */
    public function read()
    {
        $param = get_params();
		$id = isset($param['id']) ? $param['id'] : 0;
		$detail = $this->model->getDeviceById($id);
		if (!empty($detail)) {
            $detail['address'] = '';
            if (!empty($detail['user_one'])) {
                $user_one = Db::name('user_one')->where('id',$detail['user_one'])->find();
                if (!empty($user_one['pid'])) {
                    $user_two_title = Db::name('user_two')->where('id',$user_one['pid'])->value('user_name');
                    $detail['user_two_title'] = $user_two_title.'(ID：'.$user_one['pid'].')';
                } else {
                    $detail['user_two_title'] = '';
                }
                $detail['user_one_title'] = $user_one['user_name'].'(ID：'.$detail['user_one'].')';
                $detail['address'] = $user_one['address'];
            } else {
                $detail['user_one_title'] = '';
                $detail['user_two_title'] = '';
            }
            if (!empty($detail['user_three'])) {
                $user_three_title = Db::name('user_three')->where('id',$detail['user_three'])->value('user_name');
                $detail['user_three_title'] = $user_three_title.'(ID：'.$detail['user_three'].')';
            } else {
                $detail['user_three_title'] = '';
            }
			View::assign('detail', $detail);
			return view();
		}
		else{
			throw new \think\exception\HttpException(404, '找不到页面');
		}
    }

    /**
    * 删除
	* type=0,逻辑删除，默认
	* type=1,物理删除
    */
    public function del()
    {
        $param = get_params();
		$id = isset($param['id']) ? $param['id'] : 0;
		$type = isset($param['type']) ? $param['type'] : 0;

        $this->model->delDeviceById($id,$type);
   }
}
